<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/10/23  14:34
 */
namespace app\admin\controller;
class Customer extends Common
{
    /*
    *	构造函数
    */
    function __construct()
    {
        parent::__construct();
    }

    /*
    *	列表
    */
    public function lists()
    {
        $pageParam = ['query' =>[]];
        $where = [];
        $keyword = input('keyword');
        if( $keyword != ''){
            $where['customer_name'] = array('like','%'.input('keyword').'%');
            $pageParam['query']['keyword'] = $keyword;
            $this->assign('keyword', $keyword);
        }

        $customer_result = db('customer')->where($where)->order('id desc')->paginate(15,false,$pageParam)
            ->each(function($item,$key){
                $new = db('customer_info')->where(['customer_id'=>$item['id']])->order('id desc')->limit(1)->select();
                if(!empty($new)){
                    $item['new']  = $new[0];
                }
                return $item;
            });

        $this->assign('customer_result', $customer_result);
        $this->assign('page', $customer_result->render());
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch('Customer/edit');
    }

    public function edit()
    {
        if(request()->isPost()){
            $id = input('id');
            $customer_name = input('customer_name');
            $user = input('user');
            $time = input('time');
            $origin = input('origin');

            $data['customer_name'] = $customer_name;
            $data['user'] = $user;
            $data['time'] = strtotime($time);
            $data['origin'] = $origin;
//            halt($data);
            if(!$id){
                //添加
//                die($data);
                $bool = db('customer')->insert($data);
            }else{
                //修改
//                die('123');
                $bool = db('customer')->where(['id'=>$id])->update($data);
            }

            if($bool){
                $this->success('操作成功', url('Customer/lists'));
            }else{
                $this->error('操作失败');
            }
        }
        $id = input('id');
        $data = db('customer')->where(['id'=>$id])->find();
        $data['time'] = date('Y-m-d H:i:s',$data['time']);
        $this->assign('id', $id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function info()
    {
        $id = input('id');
        $data = db('customer_info')->where(['id'=>$id])->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    //编辑跟进情况
    public function editinfo()
    {
        if(request()->isPost()){
            $id = input('id');
            $title = input('title');
            $content = input('content');
            $customer_id = input('customer_id');
            $data['title'] = $title;
            $data['content'] = $content;

            if(!$id){
                //添加
                $data['customer_id'] = $customer_id;
               $bool = db('customer_info')->insert($data);
            }else{
                //修改
                $bool  = db('customer_info')->where(['id'=>$id])->update($data);
            }
            if($bool){
                $this->success('操作成功', url('Customer/lists'));
            }else{
                $this->error('操作失败');
            }
        }
        $customer_id = input('customer_id');
        $this->assign('customer_id', $customer_id);
        $id = input('id');
        $this->assign('id', $id);
        $data = db('customer_info')->where(['id'=>$id])->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function detail()
    {
        $id = input('id');
        $data = db('customer')->where(['id'=>$id])->find();
        $data['customer_info'] = db('customer_info')->field('id,title,content')->where(['customer_id'=>$id])->select();
//halt($data);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function delete()
    {
        $id = input("id");
        if(!$id) $this->error('参数有误');
        $data=db('customer')->where("id='$id'")->find();
        if(!$data) $this->error('数据不存在');
        $file =  db('customer_info')->where(['customer_id'=>$id])->select();
        if(!empty($file)){
            db('customer_info')->where(['customer_id'=>$id])->delete();
        }
        $res=db('customer')->where("id='$id'")->delete();
        if(!$res) $this->error('删除失败');
        $this->success("删除成功!",url('Customer/lists'));

    }

}